package com.kerrykidz.promotion.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.authority.service.IAuUserService;
import com.kerrykidz.dao.model.GblPosDiscountEntity;
import com.kerrykidz.promotion.dao.model.ProCampaignEntity;
import com.kerrykidz.promotion.dao.model.ProGblPrdEntity;
import com.kerrykidz.promotion.service.IPromotionService;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdEntity;
import com.kerrykidz.scminvoicing.service.IScmInvocingProductInfoService;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.service.IWorkflowService;
import com.kerrykidz.workflow.vo.OptHisVo;
import com.kerrykidz.workflow.vo.WorkflowParamVo;
import com.rdp.framework.exception.RDPException;

@RequestMapping("promotionWorkFlow")
@Controller
public class PromotionActivityControll {
	@Autowired
	private IPromotionService promotionService;
	@Autowired
	private IWorkflowService workflowService;
	@Autowired
	private IScmInvocingProductInfoService productInfoService;

	@Autowired
	private IAuUserService userService;
	
	/**
	 * 查询           流程处理        活动 数据
	 * @param entity
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月11日
	 */
	@RequestMapping("/seacheByCampIdWf")
	public ModelAndView seacheByCampId(ProCampaignEntity entity) throws RDPException{
		UserInfo user = UserUtil.getUserFromSession();
		ModelAndView model = new ModelAndView("promotion/promo_activity_new_wfsp");
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		entity = promotionService.findByCampId(entity);
		
		/**获取关联商品ID*/
		List<String> prdIdAll = promotionService.findByProCampPrdIdAll(entity.getPkCampId());
		
		/**获取关联商品*/
		
		GblScmPrdEntity proxyEntity = new GblScmPrdEntity();
		proxyEntity.setFkBranId(entity.getFkBranId());//UserUtil.getUserFromSession().getBranId()
		proxyEntity.setCateTypes("6,61");
		proxyEntity.setIspaging(true);
		proxyEntity.setIdList(prdIdAll);
		List<GblScmPrdEntity> resultList = null;
		if(prdIdAll != null && prdIdAll.size() > 0){
			resultList = productInfoService.getProductInfo(proxyEntity);
			
		}
		/*ProGblPrdEntity prdEntity = new ProGblPrdEntity();
		prdEntity.setPrdIdList(prdIdAll);
		List<ProGblPrdEntity> prdEntityList = promotionService.findLikePrdIdOrPrdName(prdEntity);*/
		
		/**获取审批流程历史*/
		List<OptHisVo> hisWorkFlowList = workflowService.findOpinions(entity.getFkFlowinstId(),user);
		
		model.addObject("entity", entity);
		model.addObject("prdIdAll", prdIdAll);
		model.addObject("prdEntityList", resultList);
		model.addObject("hisWorkFlowList", hisWorkFlowList);
		model.addObject("loginUser", UserUtil.getUserFromSession());
		
		return model;
	}
	
	/***
	 *  退回  促销流程  重新修改
	 * @param entity
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月12日
	 */
	@RequestMapping("/seacheByCampIdExtHome")
	public ModelAndView seacheByCampIdExtHome(ProCampaignEntity entity) throws RDPException{
		UserInfo user = UserUtil.getUserFromSession();
		ModelAndView model = new ModelAndView("promotion/promo_activity_edit_wfsp");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		entity = promotionService.findByCampId(entity);
		
		List<GblPosDiscountEntity>  discountAll = promotionService.findGblDiscountAll();
		
		/**获取关联商品*/
		List<String> prdIdAll = promotionService.findByProCampPrdIdAll(entity.getPkCampId());
		
		/**获取审批流程历史*/
		List<OptHisVo> hisWorkFlowList = workflowService.findOpinions(entity.getFkFlowinstId(),user);
		
		model.addObject("discountAll", discountAll);
		model.addObject("currentDate", sdf.format(new Date()));
		model.addObject("entity", entity);
		model.addObject("prdIdAll", prdIdAll);
		model.addObject("hisWorkFlowList", hisWorkFlowList);
		model.addObject("loginUser", UserUtil.getUserFromSession());
		
		model.addObject("branAll", userService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		
		return model;
	}
	/***
	 *  退回  促销流程  重新修改
	 * @param entity
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月12日
	 */

	@RequestMapping("/seacheByCampIdExtReSubmit")
	public ModelAndView seacheByCampIdExtReSubmit(ProCampaignEntity entity,@RequestParam("prdlist") List<String> prdlist) throws RDPException{
		ModelAndView model = new ModelAndView("promotion/promo_activity");
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		promotionService.updateProCampaignFlow(entity, prdlist, UserUtil.getUserFromSession());
		return model;
	}
	
	/**
	 * 查询指定促销活动
	 * @param entity
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月11日
	 */
	@RequestMapping("/seacheByCampIdWfHis")
	public ModelAndView seacheByCampIdWfHis(ProCampaignEntity entity) throws RDPException{
		UserInfo user = UserUtil.getUserFromSession();
		ModelAndView model = new ModelAndView("promotion/promo_activity_new_wfsp_his");
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		entity = promotionService.findByCampId(entity);
		
		/**获取关联商品ID*/
		List<String> prdIdAll = promotionService.findByProCampPrdIdAll(entity.getPkCampId());
		
		/**获取关联商品*/
		GblScmPrdEntity proxyEntity = new GblScmPrdEntity();
		proxyEntity.setFkBranId(entity.getFkBranId());/*UserUtil.getUserFromSession().getBranId()*/
		proxyEntity.setCateTypes("6,61");
		proxyEntity.setIspaging(true);
		proxyEntity.setIdList(prdIdAll);
		List<GblScmPrdEntity> resultList = null;
		if(prdIdAll != null && prdIdAll.size() > 0){
			resultList = productInfoService.getProductInfo(proxyEntity);
			
		}
		
		/*ProGblPrdEntity prdEntity = new ProGblPrdEntity();
		prdEntity.setPrdIdList(prdIdAll);
		List<ProGblPrdEntity> prdEntityList = promotionService.findLikePrdIdOrPrdName(prdEntity);*/
		
		/**获取审批流程历史*/
		List<OptHisVo> hisWorkFlowList = workflowService.findOpinions(entity.getFkFlowinstId(),user);
		
		model.addObject("entity", entity);
		model.addObject("prdEntityList", resultList);
		model.addObject("hisWorkFlowList", hisWorkFlowList);
		
		return model;
	}
	/***
	 * 	审批流程   提交
	 * @param param
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月12日
	 */
	@RequestMapping("/approvalProcessPromotionSub")
	public ModelAndView approvalProcessPromotionSub(WorkflowParamVo param) throws RDPException{
		ModelAndView model = new ModelAndView("promotion/promo_activity");
		
		ProCampaignEntity entity = new ProCampaignEntity();
		entity.setPkCampId(param.getBusinessKey());
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		entity = promotionService.findByCampId(entity);
		
		param.setUserInfo(UserUtil.getUserFromSession());
		
		ProcessInstance  processInstance = workflowService.complete(param);
		if(processInstance == null){
			entity.setStatus(param.getHandleAdvice().equals("1")?"3":"4");
			promotionService.updateProCampaignStatus(entity);
		}
		

		model.addObject("branAll", userService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		return model;
	}
	
	/***
	 * 	审批流程   退回
	 * @param param
	 * @return
	 * @throws RDPException 
	 * @date: 2014年9月12日
	 */
	@RequestMapping("/approvalProcessPromotionExt")
	public ModelAndView approvalProcessPromotionExt(WorkflowParamVo param) throws RDPException{
		ModelAndView model = new ModelAndView("promotion/promo_activity");
		
		ProCampaignEntity entity = new ProCampaignEntity();
		entity.setPkCampId(param.getBusinessKey());
		entity.setModiBy(UserUtil.getUserFromSession().getUserId());
		entity = promotionService.findByCampId(entity);
		
		param.setUserInfo(UserUtil.getUserFromSession());
		
		workflowService.backProcessToStart(param);
		entity.setStatus("2");
		promotionService.updateProCampaignStatus(entity);
		return model;
	}
}
